package dao.impl;

import dao.NoteDao;
import dao.NoteLabelsDao;
import pojo.Label;
import pojo.Note;
import pojo.NoteLabels;
import util.JdbcUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class NoteLabelsDaoImpl implements NoteLabelsDao {
    private boolean flag = false;
    private Connection conn = null;
    private ResultSet rs = null;
    NoteDao noteDao = new NoteDaoImpl();


    public boolean addNoteLabels(NoteLabels noteLabels) throws SQLException {
        conn = JdbcUtils.dbconn();
        Integer i = 0;
        String sql = "insert into notelabels values(0,'" + noteLabels.getLid() + "','" + noteLabels.getNid() + "')";
        i = JdbcUtils.addsql(sql);
        if (i > 0) flag = true;
        JdbcUtils.closeconn(null, null, conn);
        return flag;
    }


    public ArrayList<NoteLabels> noteLabelsList(int nid) throws SQLException, ClassNotFoundException {
        ArrayList<NoteLabels> noteLabelsList = new ArrayList<>();
        conn = JdbcUtils.dbconn();
        String sql = "select * from notelabels where nid='" + nid + "'";
        rs = JdbcUtils.selectsql(sql);
        while (true) {
            if (!rs.next()) break;
            NoteDao noteDao = new NoteDaoImpl();
            Note note = new Note();
            note = noteDao.findNote(rs.getInt("nid"));
            if (note.getStyle() == 1) {
                NoteLabels noteLabels = new NoteLabels();
                noteLabels.setNid(rs.getInt("nid"));
                noteLabels.setLid(rs.getInt("lid"));
                noteLabels.setId(rs.getInt("id"));
                noteLabelsList.add(noteLabels);

            }
        }
//        rs.last();
//        int row=rs.getRow();
//        rs.beforeFirst();
//        do{
//            NoteDao noteDao = new NoteDaoImpl();
//            Note note = new Note();
//            note = noteDao.findNote(rs.getInt("nid"));
//            if (note.getStyle() == 1) {
//                    NoteLabels noteLabels = new NoteLabels();
//                    noteLabels= (NoteLabels) DataUtils.dataEncapsulation(Class.forName("pojo.NoteLabels"),rs);
//                    noteLabelsList.add(noteLabels);
//            }
//        }while (row>0);
        JdbcUtils.closeconn(rs, null, conn);
        return noteLabelsList;

    }


    public ArrayList<NoteLabels> findNoteLabelsByLid(int lid) throws SQLException, ClassNotFoundException {
        ArrayList<NoteLabels> searchNoteLabelsByLid = new ArrayList<>();
        conn = JdbcUtils.dbconn();
        String sql = "select * from notelabels where lid='" + lid + "'";
        rs = JdbcUtils.selectsql(sql);
        while (true) {
            if (!rs.next()) break;
            NoteDao noteDao = new NoteDaoImpl();
            Note note = new Note();
            note = noteDao.findNote(rs.getInt("nid"));
            //System.out.println(rs.getInt("nid"));
            //System.out.println(note);
            if (note != null) {
                NoteLabels noteLabels = new NoteLabels();
                noteLabels.setNid(rs.getInt("nid"));
                noteLabels.setLid(rs.getInt("lid"));
                noteLabels.setId(rs.getInt("id"));
                searchNoteLabelsByLid.add(noteLabels);
            }
//                else System.out.println("wrong");
        }
//        do{
//            Note note = new Note();
//            note = noteDao.findNote(rs.getInt("nid"));
//            if (note.getStyle() == 1) {
//                NoteLabels noteLabels = new NoteLabels();
//                noteLabels= (NoteLabels) DataUtils.dataEncapsulation(Class.forName("pojo.NoteLabels"),rs);
//                searchNoteLabelsByLid.add(noteLabels);
//            }
//        }while (rs.next());
        JdbcUtils.closeconn(rs, null, conn);
        return searchNoteLabelsByLid;
    }


    public ArrayList<NoteLabels> searchNoteLabels(ArrayList<Label> searchLabels) throws SQLException, ClassNotFoundException {
        ArrayList<NoteLabels> searchNoteLabels = new ArrayList<>();
        int lid = 0;
        for (Label searchLabel : searchLabels) {
            lid = searchLabel.getId();
            ArrayList<NoteLabels> eachLabel = new ArrayList<>();
            NoteLabelsDao noteLabelsDao = new NoteLabelsDaoImpl();
            eachLabel = noteLabelsDao.findNoteLabelsByLid(lid);
            for (NoteLabels noteLabels : eachLabel) {
                searchNoteLabels.add(noteLabels);
            }
        }
        return searchNoteLabels;
    }

}
